package com.chen.pfm;
import com.chen.pfm.entity.OrmPfmStore;
import com.chen.pfm.entity.Statements;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.dataability.DataAbilityUtils;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;
import ohos.data.resultset.ResultSet;
import ohos.data.rdb.ValuesBucket;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;
import ohos.utils.PacMap;

import java.io.FileDescriptor;

public class StatementsDataAbility extends Ability {

    private static final String thisClassName="StatementsDataAbility";
    //表名
    private static final String  tableName="statements";

     static final HiLogLabel LABEL = new HiLogLabel(3, 0xD001100, thisClassName);
    //orm上下文
    private static OrmContext ormContext = null;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        HiLog.info(LABEL, thisClassName+" 已启动！");
        DatabaseHelper helper = new DatabaseHelper(this);
        ormContext = helper.getOrmContext(OrmPfmStore.DATABASE_NAME_ALIAS, OrmPfmStore.DATABASE_NAME, OrmPfmStore.class);
        if(ormContext!=null){
            HiLog.info(LABEL, "ormContext不为空");
        }
    }

    @Override
    public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
        HiLog.info(LABEL, thisClassName+" 执行查询操作");

        if(ormContext == null){
            HiLog.error(LABEL,thisClassName+"查询失败，ormContext为空");
            return null;
        }
        if(columns!=null){
            HiLog.error(LABEL,thisClassName+"的columns字段不为空");
            for(String s:columns){
                HiLog.info(LABEL,"column -->"+s);
            }
        }else{
            HiLog.error(LABEL,thisClassName+"的columns字段为空");
        }
        //查询数据库
        OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates, Statements.class);
        ResultSet resultSet = ormContext.query(ormPredicates,columns);
        if (resultSet == null){
            HiLog.info(LABEL,thisClassName+"结果集为空");
        }
        return resultSet;

    }


    @Override
    public int insert(Uri uri, ValuesBucket value) {
        if (ormContext == null) {
            HiLog.error(LABEL,thisClassName+"插入失败，ormContext为空");
            return -1;
        }
        Statements statements=new Statements();
        statements.getObjectfromValues(value);
        HiLog.info(LABEL,thisClassName+"插入数据为"+statements.toString());
        int i=-1;
        if (ormContext.insert(statements.getObjectfromValues(value))) {
            ormContext.flush();
            DataAbilityHelper.creator(this, uri).notifyChange(uri);
            HiLog.info(LABEL,thisClassName+"进行了插入操作，执行成功！");
            return (int) statements.getRowId();
        }
        HiLog.info(LABEL, "数据表"+tableName  +" ===>> 插入失败");
        return i;

    }

    @Override
    public int delete(Uri uri, DataAbilityPredicates predicates) {
        if (ormContext == null) {
            HiLog.error(LABEL,thisClassName+"删除失败，ormContext为空");
            return -1;
        }
        OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates,Statements.class);
        int value = ormContext.delete(ormPredicates);
        DataAbilityHelper.creator(this, uri).notifyChange(uri);
        HiLog.info(LABEL,thisClassName+"进行了删除操作，执行成功！");
        return value;

    }

    @Override
    public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {
        if (ormContext == null) {
            HiLog.error(LABEL,thisClassName+"更新失败，ormContext为空");
            return -1;
        }
        OrmPredicates ormPredicates = DataAbilityUtils.createOrmPredicates(predicates,Statements.class);
        int index = ormContext.update(ormPredicates, value);
        HiLog.info(LABEL, thisClassName+" 更新值为:" + index);
        DataAbilityHelper.creator(this, uri).notifyChange(uri);
        HiLog.info(LABEL,thisClassName+"进行了删除操作，执行成功！");
        return index;
    }
    @Override
    public FileDescriptor openFile(Uri uri, String mode) {
        return null;
    }

    @Override
    public String[] getFileTypes(Uri uri, String mimeTypeFilter) {
        return new String[0];
    }

    @Override
    public PacMap call(String method, String arg, PacMap extras) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}